package com.example.spinnertest.tools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.example.spinnertest.R;
import com.example.spinnertest.tools.SpinnerPopWindow.IOnItemSelectListener;

public class WXTDialog extends Dialog implements View.OnClickListener{
	
	//所有的区和学校
	String[]strCounty,strSchool;
	//获得组件
	TextView tvCounty,tvSchool;
	SpinnerPopWindow  puCountyWindow,puSchoolWindow;	
	Map<String,String[]> map = new HashMap<String, String[]>();
	String[] jimo = getContext().getResources().getStringArray(R.array.city_jimo);
	String[] laixi = getContext().getResources().getStringArray(R.array.city_laixi);
	String[] pingdu = getContext().getResources().getStringArray(R.array.city_pingdu);
	String[] jiaonan = getContext().getResources().getStringArray(R.array.city_jiaonan);
	String[] jiaoZhou = getContext().getResources().getStringArray(R.array.city_jiaozhou);
	String[] city_south = getContext().getResources().getStringArray(R.array.city_south);
	String[] city_north = getContext().getResources().getStringArray(R.array.city_north);
	String[] city_licang = getContext().getResources().getStringArray(R.array.city_licang);
	String[] city_sifang = getContext().getResources().getStringArray(R.array.city_sifang);
	String[] city_laoshan = getContext().getResources().getStringArray(R.array.city_laoshan);
	String[] city_huangdao = getContext().getResources().getStringArray(R.array.city_huangdao);
	String[] city_chengyang = getContext().getResources().getStringArray(R.array.city_chengyang);
	String []strSchContent;
	
	
	//下拉对象
	public WXTDialog(Context context) {
		super(context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		iNitTextView();

		
		puCountyWindow= new SpinnerPopWindow(getContext());
		puCountyWindow.setSpinnerAdapter(strCounty);
		
		tvCounty.setOnClickListener(this);
		tvSchool.setOnClickListener(this);
		
		itemSelect();
	}
	//资源组件初始化
	public void iNitTextView()
	{
		tvCounty = (TextView) findViewById(R.id.countyTextView);
		tvSchool  = (TextView) findViewById(R.id.schoolTextView);
		
		strCounty =getContext().getResources().getStringArray(R.array.county_item);
		
		map.put("胶州", jiaoZhou);
		map.put("即墨", jimo);
		map.put("莱西", laixi);
		map.put("胶南", jiaonan);
		map.put("平度", pingdu);
		map.put("市南区", city_south);
		map.put("市北区", city_north);
		map.put("李沧区", city_licang);
		map.put("四方区", city_sifang);
		map.put("崂山区", city_laoshan);
		map.put("黄岛区", city_huangdao);
		map.put("城阳区", city_chengyang);
	}
	
	
	public void itemSelect()
	{
		
        puCountyWindow.setItemSelectListener(new IOnItemSelectListener() {
			@Override
			public void onItemClick(int position) {
				showTextView(tvCounty, strCounty, position);
				strSchContent=map.get(strCounty[position]);
				System.out.println(strCounty[position]);
				System.out.println(map.get(strCounty[position]));
				puSchoolWindow = new SpinnerPopWindow(getContext());
				puSchoolWindow.setSpinnerAdapter(strSchContent);
				puSchoolWindow.setItemSelectListener(new IOnItemSelectListener() {
					
					@Override
					public void onItemClick(int position) {
						// TODO Auto-generated method stub
						showTextView(tvSchool, strSchContent, position);
					}
				});
				
			}
		}); 
	}
	
	
	@Override 
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		
		case R.id.countyTextView:
			showPopUpWindow(puCountyWindow,tvCounty);
			break;
		case R.id.schoolTextView:
			if(puSchoolWindow==null)
			{
				Toast.makeText(getContext(), "请先选择所在区", Toast.LENGTH_SHORT).show();
			}
			else
			{
			showPopUpWindow(puSchoolWindow,tvSchool);
			}
		    break;
		
		}
	}                                                                                                                
	
	//初始默认的数据
	public void showTextView(TextView tv,String [] str,int position)
	{
		tv.setText(str[position]);
	}
	
	public void showPopUpWindow(SpinnerPopWindow pw,View v)
	{
		pw.setWidth(v.getWidth());
		pw.showAsDropDown(v);
	}
}
